package com.junt.library.widgets;

import android.content.Context;
import android.widget.PopupWindow;

import com.junt.library.adapter.DirAdapter;
import com.junt.library.model.Media;
import com.junt.library.utils.Utils;

import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class DirPopupView extends PopupWindow {
    private final RecyclerView recyclerView;
    private DirAdapter dirAdapter;

    public DirPopupView(Context context, int width, DirAdapter.OnDirectorySelectedListener listener) {
        super(context);
        setOutsideTouchable(true);
        setWidth(width);
        recyclerView = new RecyclerView(context);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        dirAdapter = new DirAdapter(listener);
        recyclerView.setAdapter(dirAdapter);
        setContentView(recyclerView);
    }

    public void setDirMap(Map<String, List<Media>> dirMap) {
        int dirSize = dirMap.entrySet().size();
        if ( dirSize> 2) {
            setHeight(3 * (int) Utils.dp2px(getContentView().getContext(), 80));
        }else {
            setHeight(dirSize * (int) Utils.dp2px(getContentView().getContext(), 80));
        }
        dirAdapter.setNewData(dirMap);
    }
}
